package student.aistar.day01;

import java.sql.*;

public class HelloJdbcDemo {
    public static void main(String[] args) {
        //注册驱动
        Connection connection = null;
        Statement statement = null;
                ResultSet result = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //获取链接
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/j03s", "root","Abc1234!");
            //获取prepareStatement语句
            statement = connection.createStatement();
            String sql = "select id, first_name from s_emp";
            result = statement.executeQuery(sql);
            while (result.next()){
                int id = result.getInt(1);
                String first_name = result.getString("first_name");
                System.out.println(id + "\t" + first_name);
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                result.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
